package com.swt.gap.controller;

import com.swt.gap.model.entity.ModelWithBLOBs;
import com.swt.gap.model.vo.ModelByClassVO;
import com.swt.gap.model.vo.ModelVO;
import com.swt.gap.service.ModelService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Api(tags = "模型")
@RestController
@RequestMapping("/model")
@Slf4j
public class ModelController {

    private final ModelService modelService;

    public ModelController (ModelService modelService) {
        this.modelService = modelService;
    }

    @PostMapping
    @ApiOperation(value = "创建", notes = "创建模型")
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED", response = ModelVO.class)})
    public ResponseEntity saveModel(@RequestBody ModelVO model) {
        int userId = UserLoginUtils.getUserId();
        model.setDeleted(false);
        model.setCreatedBy(userId);
        model.setCreatedTime(new Date());
        modelService.saveModel(model);
        return ResponseEntity.ok(model);
    }

    @GetMapping("/{modelId}")
    @ApiOperation(value = "根据id获取模型", notes = "根据id获取模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ModelWithBLOBs.class)})
    public ResponseEntity getModel(@PathVariable Integer modelId) {
        ModelVO retValue = modelService.getModelById(modelId);
        if (null == retValue) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到该模型");
        }
        return ResponseEntity.ok(retValue);
    }

    @PutMapping("/{modelId}")
    @ApiOperation(value = "修改模型", notes = "修改模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ModelVO.class)})
    public ResponseEntity updateModel(@PathVariable Integer modelId, @RequestBody ModelVO model) {
        int userId = UserLoginUtils.getUserId();
        model.setModelId(modelId);
        model.setUpdatedBy(userId);
        model.setUpdatedTime(new Date());
        modelService.updateModel(model);
        return ResponseEntity.ok(model);
    }

    @DeleteMapping("/{modelId}")
    @ApiOperation(value = "删除模型", notes = "删除模型")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity deleteModel(@PathVariable Integer modelId) {
        int userId = UserLoginUtils.getUserId();
        ModelVO model = new ModelVO();
        model.setModelId(modelId);
        model.setDeleted(true);
        model.setUpdatedBy(userId);
        model.setUpdatedTime(new Date());
        modelService.updateModel(model);
        return ResponseEntity.ok(true);
    }


    @GetMapping
    @ApiOperation(value = "获取模型列表", notes = "获取模型列表")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = ModelByClassVO.class, responseContainer = "List")})
    public ResponseEntity listModel() {
        return ResponseEntity.ok(modelService.listModel());
    }
}
